namespace SimpleWebServices.Web.Routing
{
    using System;
    using System.Configuration;
    using System.Web;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using Xunit;

    public sealed class FileRouteFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<FileRoute>()
                .DerivesFrom<RouteBase>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new FileRoute());
        }

        [Fact]
        public void op_GetRouteData_HttpContextBaseNull()
        {
            Assert.Throws<ArgumentNullException>(() => new FileRoute().GetRouteData(null));
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase()
        {
            FileConfiguration.Settings().Clear();

            try
            {
                FileConfiguration.Settings().Add("<file content-type='text/plain' path='/example.txt' />".Deserialize<FileSetting>());

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example.txt").Verifiable();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/example.txt").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();

                RouteData actual = new FileRoute().GetRouteData(mock.Object);

                Assert.Equal<string>("example.txt", (actual.Route as Route).Url);
                Assert.Equal<string>("File", (string)actual.Values["controller"]);
                Assert.Equal<string>("Representation", (string)actual.Values["action"]);

                mock.VerifyAll();
            }
            finally
            {
                ConfigurationManager.RefreshSection("simplewebservices/files");
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenNotFound()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example.txt").Verifiable();

                RouteData actual = new FileRoute().GetRouteData(mock.Object);

                Assert.Null(actual);

                mock.VerifyAll();
            }
            finally
            {
                ConfigurationManager.RefreshSection("simplewebservices/files");
            }
        }

        [Fact]
        public void op_GetVirtualPath_RequestContextNull_RouteValueDictionary()
        {
            Assert.Throws<ArgumentNullException>(() => new FileRoute().GetVirtualPath(null, new RouteValueDictionary()));
        }

        [Fact]
        public void op_GetVirtualPath_RequestContext_RouteValueDictionary()
        {
            FileConfiguration.Settings().Clear();

            try
            {
                FileConfiguration.Settings().Add("<file content-type='text/plain' path='/example.txt' />".Deserialize<FileSetting>());

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example.txt").Verifiable();

                RequestContext request = new RequestContext(mock.Object, new RouteData());

                VirtualPathData actual = new FileRoute().GetVirtualPath(request, new RouteValueDictionary());

                Assert.NotNull(actual);

                mock.VerifyAll();
            }
            finally
            {
                ConfigurationManager.RefreshSection("simplewebservices/files");
            }
        }

        [Fact]
        public void op_GetVirtualPath_RequestContext_RouteValueDictionary_whenNotFound()
        {
            var mock = new Mock<HttpContextBase>();
            mock.SetupGet<string>(x => x.Request.Path).Returns("/example.txt").Verifiable();

            RequestContext request = new RequestContext(mock.Object, new RouteData());

            VirtualPathData actual = new FileRoute().GetVirtualPath(request, new RouteValueDictionary());

            Assert.Null(actual);

            mock.VerifyAll();
        }
    }
}